import os
import string
import sys

if __name__ == '__main__':
	# CHANGE THE ROOT FOLDER
	root = "C:\\Users\\Zinho\\Downloads\\Amazon"
	wordcount = {}
	
	for subdir, dirs, files in os.walk(root):
		newPctg = 0
		oldPctg = -1
		i = 0
		
		cats = sys.argv[1:]
		
		if len(cats) == 0:
			process = True
		else:
			process = False
			if ("AirConditioners" in subdir) and ('a' in cats):
				process = True
			elif ("CanisterVacuums" in subdir) and ('v' in cats):
				process = True
			elif ("CoffeeMachines" in subdir) and ('c' in cats):
				process = True
			elif ("DigitalSLRs" in subdir) and ('d' in cats):
				process = True
			elif ("Laptops" in subdir) and ('l' in cats):
				process = True
			elif ("MP3Players" in subdir) and ('m' in cats):
				process = True
			elif ("SpaceHeaters" in subdir) and ('h' in cats):
				process = True
		
		if process:
			print subdir
			
			for file in files:
				fullpath = os.path.join(subdir, file)
				
				f = open(fullpath, 'r')
				previousWord = ""
				record = False
				
				for line in f:
					for word in line.lower().translate(string.maketrans("",""), string.punctuation).split():
						if previousWord == "content":
							record = True
						elif word == "id":
							record = False
						
						if record:
							if word not in wordcount.keys():
								wordcount[word] = 1
							else:
								wordcount[word] += 1
						
						previousWord = word
						
				f.close()
				
				i += 1
				newPctg = int(float(i)/len(files) * 100)
				if newPctg != oldPctg and newPctg % 10 == 0:
					print newPctg,"%"
				oldPctg = newPctg
	
	print "Processig results..."
	wordList = wordcount.items()
	print ">> Sorting list..."
	wordList.sort(key = lambda word:word[1])
	wordList.reverse()
	
	print "Saving output file..."
	f = open("wordcount.txt", 'w')
	for stopword in wordList:
		f.write(str(stopword[0] + " " + str(stopword[1]) + "\n"))
	f.close()